package com.chenjiahao.kaiyanwan.ui.discovery

import android.app.Activity
import android.content.Context
import android.content.Intent
import android.view.View
import com.bumptech.glide.Glide
import com.bumptech.glide.load.resource.drawable.DrawableTransitionOptions
import com.bumptech.glide.request.RequestOptions
import com.chenjiahao.kaiyanwan.BUNDLE_VIDEO_DATA
import com.chenjiahao.kaiyanwan.R
import com.chenjiahao.kaiyanwan.base.CommonAdapter
import com.chenjiahao.kaiyanwan.base.ViewHolder
import com.chenjiahao.kaiyanwan.durationFormat
import com.chenjiahao.kaiyanwan.startActivity
import com.chenjiahao.kaiyanwan.ui.videodetail.VideoDetailActivity
import com.hazz.kotlinmvp.mvp.model.bean.HomeBean

/**
 * Created by chenjh on 2020/9/18
 */
class CategoryDetailAdapter(context: Context, dataList: ArrayList<HomeBean.Issue.Item>, layoutId: Int)
    : CommonAdapter<HomeBean.Issue.Item>(context, dataList, layoutId) {


    fun addData(dataList: ArrayList<HomeBean.Issue.Item>) {
        this.mData.addAll(dataList)
        notifyDataSetChanged()
    }


    override fun bindData(holder: ViewHolder, data: HomeBean.Issue.Item, position: Int) {
        setVideoItem(holder, data)
    }

    /**
     * 加载 content item
     */
    private fun setVideoItem(holder: ViewHolder, item: HomeBean.Issue.Item) {
        val itemData = item.data
        val cover = itemData?.cover?.feed?:""
        // 加载封页图
        Glide.with(mContext)
            .load(cover)
            .apply(RequestOptions().placeholder(R.drawable.placeholder_banner))
            .transition(DrawableTransitionOptions().crossFade())
            .into(holder.getView(R.id.iv_image))
        holder.setText(R.id.tv_title, itemData?.title?:"")

        // 格式化时间
        val timeFormat = durationFormat(itemData?.duration)

        holder.setText(R.id.tv_tag, "#${itemData?.category}/$timeFormat")

        holder.setOnItemClickListener(listener = View.OnClickListener {
            startActivity<VideoDetailActivity>(mContext as Activity, holder.getView(R.id.iv_image)){
                putExtra(BUNDLE_VIDEO_DATA, item)
                putExtra(VideoDetailActivity.Companion.TRANSITION, true)
            }
        })


    }



}